package com.colorful.insuranceproject.microDrug;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.SendVerifyCodeActivity;
import com.colorful.insuranceproject.bean.VerifyBean;
import com.colorful.insuranceproject.commonActivity.WebViewActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.request.MicroDrugRequest;
import com.lovely3x.common.utils.CommonUtils;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 支付密码绑定页面  第一步
 */
public class PayPwdBindActivity extends SendVerifyCodeActivity {

    private static final int REQUEST_CODE = 10000;

    private CountDownDesc mCountDownDesc;

    private static final int GET_VERIFY_CODE_URL = 0X1;

    private static final int BIND_PAY_PWD_URL = 0X2;

    private MicroDrugRequest microDrugRequest;

    /**
     * 姓名
     */
    @Bind(R.id.et_name)
    EditText etName;

    /**
     * 身份证号
     */
    @Bind(R.id.et_id_card)
    EditText etIdCard;

    /**
     * 绑定手机号
     */
    @Bind(R.id.et_phone)
    EditText etPhone;

    /**
     * 验证码
     */
    @Bind(R.id.tv_verifyCode)
    EditText tvVerifyCode;

    /**
     * 获取验证码
     */
    @Bind(R.id.tv_get_verifyCode)
    TextView tvGetVerifyCode;

    private VerifyBean bean;

    @Override
    protected int getContentView() {
        return R.layout.activity_pay_pwd_bind;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.pay_pwd));
    }

    @Override
    protected void onViewInitialized() {
        microDrugRequest = new MicroDrugRequest(getHandler());
        mCountDownDesc = new CountDownDesc(tvGetVerifyCode.getId(), DEFAULT_COUNT_DOWN_TIME, 100);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            //  获取短信验证码
            case GET_VERIFY_CODE_URL:
                dismissProgressCircle();
                if (response.isSuccessful) {
                    launchActivityForResult(PayPwdSetActivity.class, REQUEST_CODE, PayPwdSetActivity.EXTRA_ID_CARD, etIdCard.getText().toString().trim());
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            case BIND_PAY_PWD_URL:
                if (response.isSuccessful) {
                    bean = (VerifyBean) response.obj;
                    startCountDownTimerIfNeeded();
                    showToast(getString(R.string.verifyCode_send_success));
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                    sendVerifyCodeFailure(mCountDownDesc);
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick({R.id.tv_claims_set_encounter_question, R.id.tv_set, R.id.tv_get_verifyCode})
    void OnClicked(View view) {
        String card = etIdCard.getText().toString().trim();
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String code = tvVerifyCode.getText().toString().trim();
        switch (view.getId()) {
            //  理赔设置遇到问题
            case R.id.tv_claims_set_encounter_question:
                String url = URLConst.CANCEL_CLAIMS_URL;
                launchActivity(WebViewActivity.class, WebViewActivity.EXTRA_LOAD_URL, url);
                break;
            // 下一步
            case R.id.tv_set:
                if (!card.equals("") && !name.equals("") && !phone.equals("") && !code.equals("") && bean != null && !bean.getMessageId().equals("")) {
                    showProgressCircle();
                    microDrugRequest.judgePayPwdUrl(card, name, phone, code, bean.getMessageId(), GET_VERIFY_CODE_URL);
                } else {
                    showToast(getString(R.string.please_compleate_infomation));
                }
                break;
            //  获取验证码
            case R.id.tv_get_verifyCode:
                if (CommonUtils.isMobilePhone(phone)) {
                    sendVerifyCode(mCountDownDesc.reset());
                    microDrugRequest.getBindPayPwdUrl(phone, BIND_PAY_PWD_URL);
                } else {
                    showToast(getString(R.string.please_enter_the_correct_phone_number));
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            finish();
        }
    }
}
